package snaptest.tool.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import snaptest.repo.FileRepo;
import snaptest.repo.RepoManager;

public class FileDiffModel {

	private final PlainDocument refDoc = new PlainDocument();
	
	private final PlainDocument newDoc = new PlainDocument();
	
	private FileRepo refRepo = RepoManager.getReferenceRepo();
	private FileRepo tmpRepo = RepoManager.getTemporaryRepo();
	
	
	public void setSelectedFile(String selectedFile) {
		setDocumentContent(newDoc, tmpRepo.load(selectedFile));
		setDocumentContent(refDoc, refRepo.load(selectedFile));
	}

	private static void setDocumentContent(PlainDocument doc, String fileContent) {
		try {
			doc.replace(0, doc.getLength(), fileContent, null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	public PlainDocument getRefDoc() {
		return refDoc;
	}

	public PlainDocument getNewDoc() {
		return newDoc;
	}
	
	public PropertyChangeListener getChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				setSelectedFile((String) evt.getNewValue());
			}
		};
	}
}
